package com.fxkj.cloud.drms.ui.mqtt.handler.receive;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fxkj.cloud.dbs.common.model.response.MqttResponse;
import com.fxkj.cloud.drms.application.DeviceApplicationService;
import com.fxkj.cloud.drms.infra.mq.mqtt.MqttConstants;
import com.fxkj.cloud.drms.ui.mqtt.handler.AbstractReceiveHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/24 13:08
 */
@Component(MqttConstants.Topic.Receive.DEVICE_PLAN_COMMAND_RESPONSE_SERVICE_NAME)
@Slf4j
public class DevicePlanCommandResponseHandler extends AbstractReceiveHandler<MqttResponse<Boolean>> {

    private final DeviceApplicationService deviceApplicationService;

    public DevicePlanCommandResponseHandler(DeviceApplicationService deviceApplicationService) {
        this.deviceApplicationService = deviceApplicationService;
    }

    @Override
    protected void doHandler(String topic, MqttResponse<Boolean> data, String... topicParams) throws Exception {
        log.debug("[DevicePlanCommandResponseHandler] -| doHandler -| topicParams: [{}], topic: {}, data: {}", topicParams, topic, data);
        String deviceSerialNo = topicParams[0];
        UUID requestId = data.getRequestId();
        if (null == requestId) {
            log.warn("requestId is null, topic: {}, data: {}", topic, data);
            return;
        }
        //如果相应成功
        if (data.getCode().equals(200)) {
            deviceApplicationService.deviceControlPlanCallback(data.getRequestId(), deviceSerialNo);
        }
    }

    @Override
    protected TypeReference<MqttResponse<Boolean>> getType() {
        return new TypeReference<MqttResponse<Boolean>>() {
        };
    }
}
